package cn.school.student.service.impl;

import cn.school.common.BaseVO;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.EntranceProcessDao;
import cn.school.student.service.EntranceProcessService;
import cn.school.student.vo.EntranceProcessVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class EntranceProcessServiceImpl extends ServiceImpl<EntranceProcessDao, EntranceProcessVO> implements EntranceProcessService {

    @Override
    public IPage<EntranceProcessVO> findEntranceProcessPageList(EntranceProcessVO vo) {
        LambdaQueryWrapper<EntranceProcessVO> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getEntranceType())){
            wrapper.eq(EntranceProcessVO::getEntranceType,vo.getEntranceType());
        }else{
            wrapper.select(EntranceProcessVO::getTitle,EntranceProcessVO::getSemester,
                    BaseVO::getCreateTime,EntranceProcessVO::getStatus,BaseVO::getId,EntranceProcessVO::getEntranceType);
        }

        if(!StringUtils.isEmpty(vo.getTitle())){
            wrapper.like(EntranceProcessVO::getTitle,vo.getTitle());
        }
        if(!StringUtils.isEmpty(vo.getSemester())){
            wrapper.eq(EntranceProcessVO::getSemester,vo.getSemester());
        }
        if(!StringUtils.isEmpty(vo.getStatus())){
            wrapper.eq(EntranceProcessVO::getStatus,vo.getStatus());
        }
        wrapper.orderByDesc(EntranceProcessVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public int addOrUpdateEntranceProcess(EntranceProcessVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        int num=0;
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreateBy(username);
            vo.setCreateTime(new Date());
            num=this.baseMapper.insert(vo);
        }else{
            vo.setModifyBy(username);
            vo.setModifyTime(new Date());
            num=this.baseMapper.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteEntranceProcessBatch(List<Long> ids) {
        int num=0;
        for (Long id:ids){
            this.baseMapper.deleteById(id);
            num++;
        }
        return num;
    }

    @Override
    public EntranceProcessVO findEntranceProcessDetailById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int batchUpdateStatus(List<EntranceProcessVO> voList) {
        int num=0;
        for(EntranceProcessVO processVO:voList){
            this.baseMapper.updateById(processVO);
            num++;
        }
        return num;
    }
}
